//
//  PKMineViewController.swift
//  GoAbroad
//
//  Created by Pink Snow on 2025/5/15.
//

import UIKit

import UIKit

class PKMineViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {

    var  tableView : UITableView!
    private let headerHeight: CGFloat = 240
    private let headerMinHeight: CGFloat = 88 + 120

    private var headerView: UIView!
    private var headerImageView: UIImageView!
    private var nameLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        setupTableView()
        setupHeaderView()
        self.navigationController?.isNavigationBarHidden  = true
    }

    private func setupTableView() {
        tableView = UITableView(frame: .init(x: 0, y: headerHeight, width: DeviceHelper.screenWidth, height: DeviceHelper.screenHeight-headerHeight), style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
//        tableView.frame =
//
        tableView.scrollIndicatorInsets = tableView.contentInset
        view.addSubview(tableView)
    
      
        
        
    }

    private func setupHeaderView() {
        // Header 背景容器
        headerView = UIView(frame: CGRect(x: 0, y:0, width: DeviceHelper.screenWidth, height: headerHeight))
        headerView.backgroundColor = .clear
        
        // 背景图
        headerImageView = UIImageView(frame: headerView.bounds)
        headerImageView.image = UIImage(named: "backImage") ?? UIImage(systemName: "person.crop.circle.fill")!
        headerImageView.contentMode = .scaleAspectFill
        headerImageView.clipsToBounds = true
        headerView.addSubview(headerImageView)

        // 用户昵称
        nameLabel = UILabel()
        nameLabel.text = "Ghibli 风用户"
        nameLabel.textColor = .white
        nameLabel.font = UIFont.boldSystemFont(ofSize: 24)
        nameLabel.textAlignment = .center
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        headerView.addSubview(nameLabel)

        // Auto Layout 约束
        NSLayoutConstraint.activate([
            nameLabel.centerXAnchor.constraint(equalTo: headerView.centerXAnchor),
            nameLabel.bottomAnchor.constraint(equalTo: headerView.bottomAnchor, constant: -24)
        ])
        
        view.addSubview(headerView)
        
        tableView.register(UINib.init(nibName: "PKMineTableViewCell", bundle: nil), forCellReuseIdentifier: "PKMineTableViewCell")
        
        
    }

    // MARK: - TableView DataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "PKMineTableViewCell", for: indexPath)
//        cell.textLabel?.text = "Row \(indexPath.row + 1)"
        return cell
    }

    // MARK: - ScrollView Delegate

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetY = scrollView.contentOffset.y + headerHeight
       print(offsetY)
        if offsetY < headerHeight {
            // 下拉放大
            let scale = abs(headerHeight - offsetY) + headerHeight
            headerImageView.frame = CGRect(x: -offsetY / 2, y:  0 , width: DeviceHelper.screenWidth + abs(offsetY), height: scale)
        } else {
            // 吸顶时保持固定高度
//            var newHeight = headerHeight - offsetY
//            newHeight = max(headerMinHeight, newHeight)
//            headerView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: newHeight)
//            headerImageView.frame = headerView.bounds
        }
    }
}
